<?php
/**
 * Created by PhpStorm.
 * User: tt
 * Date: 2018/1/13
 * Time: 11:15
 */

namespace app\admin\controller;

use app\admin\logic\Origin as Origin_logic;
use think\Cache;
use think\Db;

class Origin extends Admin
{
    /**
     * 来源列表
     */
    public function index()
    {
        $Origins = new Origin_logic();
        $origin = $Origins->getAllOriginToTree();
        foreach ($origin as $k => $v) {
            $origin[$k]['qdtype'] = Db::name('qudao')->where('id', $v['qdtype'])->field('title')->find();
            $origin[$k]['area'] = Db::name('area')->where('id', $v['area_id'])->field('name')->find();
        }
        $this->assign('origin', $origin);
        return $this->fetch();
    }

    /**
     * 新增来源
     */
    public function add()
    {
        $Origin_logic = new Origin_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $data['title'] = trim($data['title']);
            $data['create_time'] = time();
            if ($data['type'] == 1) {
                $data['area_id'] = 0;
                $data['qdtype'] = 0;
                $result = $Origin_logic->addOrigin($data);
            } else {
                $result = $Origin_logic->addOrigin($data);
            }
            if (false === $result['result']) {
                $this->error('新增失败');
            } else {
                $this->success('新增成功');
            }
        } else {
            $area = Db::name('area')->where('status', 1)->field('id,name,type')->select();
            $leibie = Db::name('qudao')->where('type', 2)->field('id,title')->select();
            $this->assign('area', $area);
            $this->assign('leibie', $leibie);
        }
        return $this->fetch();
    }

    /**
     * 修改来源
     */
    public function edit()
    {
        $Origin_logic = new Origin_logic();
        $arr = $Origin_logic->getAllText();
        $this->assign('pid', $arr);
        if ($this->request->isPost()) {
            $data = input('post.');
            if ($data['type'] == 1) {
                $data['area_id'] = 0;
                $data['qdtype'] = 0;
                $result = $Origin_logic->editOrigin($data);
            } else {
                $result = $Origin_logic->editOrigin($data);
            }
            if ($result['result'] === true) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = input('id');
            $origin = Db::name('origin')->where('id', $id)->find();
            $area = Db::name('area')->where('status', 1)->field('id,name,type')->select();
            $leibie = Db::name('qudao')->where('type', 2)->field('id,title')->select();
            $this->assign('area', $area);
            $this->assign('leibie', $leibie);
            $this->assign('origin', $origin);
            return $this->fetch();
        }
        return $this->fetch();
    }

    /**
     * 删除来源
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $Origin_logic = new Origin_logic();
        $result = $Origin_logic->delOrigin($id);
        if ($result['result'] === true) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

//    public function set_states($id = '')
//    {
//        if ($this->request->isPost()) {
//            $data = $this->request->post();
//            $data['rule'] = implode(',',$data['rule']);
//            db('origin')->where('id',$data['id'])->update(['rule'=>$data['rule']]);
//            $this->success('绑定成功');
//        }elseif ($this->request->isGet()){
//            if (empty($id)){
//                $this->error('参数丢失');
//            }
//            $arr = db('origin')->where('id',$id)->find();
//            $data = Db::name('banji')->cache(6000)->select();
//            $this->assign('data',$data);
//            $this->assign('arr',$arr);
//            return $this->fetch();
//        }
//
//    }
}